import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    menuList: [],
    menuIndexData: {},
    breadCrunbData: {},
    shoporderData:{},
    shopdetailData:{},
    permissions: []
  },
  getters: {
    secondMenuList: state => {
      if (state.menuIndexData.pIndex == -1) {
        return []
      } else {
        try {
          return state.menuList[state.menuIndexData.pIndex].child
        } catch (error) {
          return []
        }
      }
    },
    navNum: state => {
      let arr = []
      let num = 0
      if (state.menuIndexData.pIndex == -1) {
        arr = []
      } else {
        try {
          arr = state.menuList[state.menuIndexData.pIndex].child
        } catch (error) {
          arr = []
        }
      }
      arr.map(obj=>{
        if (obj.hideInMenu) {
          obj.child && obj.child.map(item=>{
            !item.hideInMenu && num++
          })
        } else {
          num++
        }
      })
      return num
    },
    shoporderData:state =>{
       return state.shoporderData
    },
    shopdetailData:state =>{
      return state.shopdetailData
    },
    permissions:state =>{
      return state.permissions
    }
  },
  mutations: {
    setMenuIndex(state, data) {
      state.menuIndexData = data
    },
    setPermissions(state, data) {
      state.permissions = data
    },
    setMenu(state, list) {
      state.menuList  = list
    },
    setBreakData(state, data) {
      state.breadCrunbData  = data
    },
    setShopOrder(state,data){
        state.shoporderData = data
    },
    setShopDetail(state,data){
      state.shopdetailData = data
    }
  },
  actions: {
    setMenuList(context,list) {
      context.commit('setMenu', list)
    },
    setPermission(context,list) {
      context.commit('setPermissions', list)
    },
    setMenuIndex(context, data) {
      context.commit('setMenuIndex', data)
    },
    setBreak(context, data) {
      context.commit('setBreakData', data)
    },

    setShop(context,data){
        context.commit('setShopOrder',data)
    },
    setDetail(context,data){
       context.commit('setShopDetail',data)
    }
  },
  modules: {
  }
})
